package com.baidu.interceptor;

import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;

import com.baidu.config.MainConfig;
import com.baidu.util.CommonUtil;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import com.jfinal.kit.PropKit;

public class MainInterceptor implements Interceptor{
	
	public static final String homeUrl = MainConfig.custom_prop.get("home_url");//首页、域名
	public static final String websiteName = MainConfig.custom_prop.get("website_name");//站点名称
	public static final String version = MainConfig.custom_prop.get("version");//站点名称
	public static final int install = MainConfig.custom_prop.getInt("install");//安装状态
	
	@Override
	public void intercept(Invocation ai) {
		Controller c = ai.getController();
		HttpServletRequest request = c.getRequest();
		//设置全局信息
		c.setAttr("base", request.getContextPath());
		c.setAttr("home", homeUrl);
		try {
			c.setAttr("website_name", new String(websiteName.getBytes("ISO-8859-1"), "UTF-8"));
			c.setAttr("version",  new String(version.getBytes("ISO-8859-1"), "UTF-8"));
		} catch (UnsupportedEncodingException e) {
		}
		if(install == 0){//第一次访问程序 需要安装
			c.redirect(CommonUtil.getHostPath(c.getRequest())+"install");
			return;
		}else{
			ai.invoke();
		}
	}
}
